package com.example.springbott.wx_service;

import com.example.springbott.bean.*;
import com.example.springbott.mapper.MarketKeywordMapper;
import com.example.springbott.mapper.MarketSearchHistoryMapper;
import com.example.springbott.util.StringUtil;
import com.example.springbott.utils.StringUtils;
import com.example.springbott.vo.KeyWordVo;
import com.example.springbott.vo.WxSearchIndex;
import com.example.springbott.wx_service.WxSearchService;
import jdk.nashorn.internal.ir.CallNode;
import org.apache.shiro.SecurityUtils;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 付常莹
 * @description: TODO
 * @date 2022/11/22 11:24
 */
@Service
public class WxSearchServiceIml implements WxSearchService {
    private  Integer isDefault = 1;
    private Integer isHot =1;
    private Integer deleted = 1;
    private String from = "wx";
    @Resource
    MarketKeywordMapper marketKeywordMapper;
    @Resource
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    /**
     * 该方法用于关键词搜索
     * @param
     * @return
     */
//     @Override
//     public List<MarketKeyword> helper(MarketKeyword marketKeyword) {
//         MarketKeywordExample keywordExample = new MarketKeywordExample();
//         MarketKeywordExample.Criteria criteria = keywordExample.createCriteria();
//         if (StringUtils.isEmpty(marketKeyword.getKeyword())){
//             criteria.andKeywordLike(marketKeyword.getKeyword());
//         }
//        //封装查询到的数据
//         List<MarketKeyword> data = marketKeywordMapper.selectByExample(keywordExample);
//
// //        //将搜索的历史记录插入历史记录表
// //        long l = System.currentTimeMillis();
// //        Date date = new Date(l);
// //        String keyword = marketKeyword.getKeyword();
// //        MarketSearchHistory searchHistory = new MarketSearchHistory();
// //        searchHistory.setAddTime(date);
// //        searchHistory.setUpdateTime(date);
// //        searchHistory.setKeyword(keyword);
// //        searchHistory.setDeleted(false);
// //        searchHistory.setUserId(01);
// //        searchHistory.setFrom(from);
// //        marketSearchHistoryMapper.insertSelective(searchHistory);
//
//         //根据输入的搜索词返回相关信息
//         return data;
//     }

    @Override
    public List<String> helper(String keyword) {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        criteria.andKeywordLike("%" + keyword + "%");
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);


        List<String> arrayList = new ArrayList<>();


        for (MarketKeyword marketKeyword : marketKeywords) {
            arrayList.add(marketKeyword.getKeyword());
        }
        return arrayList;

    }

    /**
     * 该方法用于输入框输入搜索
     * @return
     */
    @Override
    public WxSearchIndex index() {
        ArrayList<KeyWordVo> keyWordVos1 = new ArrayList<>();
        //返回默认关键词相关
        MarketKeyword selectDefault = marketKeywordMapper.selectDefault(isDefault);
        //查出热词的id，根据id查出记录
        ArrayList<MarketKeyword> hotKeywords = new ArrayList<MarketKeyword>();
        Integer[]  id =  marketKeywordMapper.selectHotId(isHot);
        for (Integer integer : id) {
            MarketKeyword marketKeyword = marketKeywordMapper.selectByPrimaryKey(integer);
            hotKeywords.add(marketKeyword);
        }
        //判断用户是否登录，登录才返回历史记录
        MarketUser principal = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        //查找历史记录，返回list
        if (principal!=null){
        keyWordVos1 = marketKeywordMapper.selectAllKeyword();
            WxSearchIndex data = new WxSearchIndex(selectDefault, keyWordVos1, hotKeywords);
            return data;
        }

        WxSearchIndex data = new WxSearchIndex(selectDefault, keyWordVos1, hotKeywords);
        return data;
    }

    /**
     * 逻辑删除历史搜索记录
     */
    @Override
    public void delete() {
        marketKeywordMapper.updateDelete(deleted);
    }

}
